package cate.common.table.guild.treasurehouse.core;

import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@NoArgsConstructor
@AllArgsConstructor
@NoteClass("宝库掉落物品")
public class TreasureItem {
    @NoteField("宝库配置ID")
    public int tid;
    @NoteField("数量")
    public int num;

    /**
     * 拷贝
     */
    public TreasureItem copy() {
        TreasureItem item = new TreasureItem();
        item.tid = tid;
        item.num = num;
        return item;
    }

    @Override
    public String toString() {
        return tid + "-" + num;
    }

    /**
     * 解析列表
     */
    public static List<TreasureItem> parse(String str) {
        if (StringUtils.isBlank(str)) {
            return Collections.emptyList();
        }
        try {
            List<TreasureItem> retList = new ArrayList<>();
            String[] splits = str.split("#");
            for (String sp : splits) {
                String[] sub = sp.split(":");
                retList.add(new TreasureItem(Integer.parseInt(sub[0]), Integer.parseInt(sub[1])));
            }
            return retList;
        } catch (Exception e) {
            log.error("", e);
            return Collections.emptyList();
        }
    }
}
